package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.DateUtil;
import com.item.dao.model.QuestionRecommend;
import com.item.dao.model.QuestionRecommendExample;
import com.item.dao.model.QuestionRecommendExample.Criteria;
import com.item.exception.JSONRepUtils;
import com.item.service.QuestionRecommendService;
import com.item.util.SubjectUtils;

@Controller
public class RecommendController {
	@Autowired
	private QuestionRecommendService recommendService;
	
	@RequestMapping("/listRecommend")
	@ResponseBody
	public String list(Page param,QuestionRecommend recommend) throws Exception{
		QuestionRecommendExample example = new QuestionRecommendExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);
		Criteria criteria = example.createCriteria();
		
		//以下两个字段用来做筛选  分别是title和source的模糊匹配
		if(StringUtils.isNotBlank(recommend.getTitle())){
			criteria.andTitleLike("%"+recommend.getTitle()+"%");
		}
		if(StringUtils.isNotBlank(recommend.getRemark())){
			criteria.andRemarkLike("%" + recommend.getRemark() +"%");
		}
		if(StringUtils.isNotBlank(recommend.getSubject())){
			criteria.andSubjectEqualTo(recommend.getSubject());
		}
		if(recommend.getTime() != null){
			criteria.andTimeEqualTo(recommend.getTime());
		}
		example.setOrderByClause("time DESC,title DESC");
		List<QuestionRecommend> list = recommendService.selectByExample(example);
		return JSONRepUtils.serialize(new Page(PaginationSupport.getContext().getTotalCount(), list), 0);
	}
	
	@RequestMapping("/showlistRecommend")
	public String list() {
		return "recommend/recommendList";
	}
	
	@RequestMapping("/toEditRecommend")
	public String toEdit()throws Exception{
		return "/recommend/recommendEdit";
	}
	
	@RequestMapping(value="/saveRecommend",method=RequestMethod.POST)
	@ResponseBody
	public String save(QuestionRecommend question, HttpSession session)throws Exception{
		//WebPageFileController fileController = new WebPageFileController("bs", "news", "utf8");
//		System.out.println(date);
		question.setCreateTime(question.getTime());
		try{
			if(StringUtils.isBlank(question.getId())){
				question.setIsAd(0);
				question.setUserid("");
				question.setUrl("");
				question.setType(SubjectUtils.getTyepBySubject(question.getSubject()));
				recommendService.insert(question);
			}else{
				question.setModifyTime(new Date());
				recommendService.updateByPrimaryKeySelective(question);
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdRecommend")
	@ResponseBody
	public String findById(String id,HttpSession session)throws Exception{		
		QuestionRecommend n = recommendService.selectByPrimaryKey(id);
		return JSONRepUtils.serialize(n, 0);
	}
	
	@RequestMapping("/delRecommend")
	@ResponseBody
	public String del(String id,String url,HttpSession session)throws Exception{
//		WebPageFileController fileController = new WebPageFileController();
		String[] ids = id.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			recommendService.deleteByPrimaryKey(ids[i]);
//			fileController.deleteFile(urls[i]);
		}
		return "{\"success\":true}";
	}
	
	@RequestMapping("/topSetRecommend")
	@ResponseBody
	public String topSet(String id,HttpSession session) throws Exception {
		return "{\"success\":true}";
	}
	
	@RequestMapping("/topDelRecommend")
	@ResponseBody
	public String topDel(String id,HttpSession session) throws Exception {
		return "{\"success\":true}";
	}
}

